# encoding: utf-8
# @File  : test05_edit_addr.py
# @Author: wu shaofan
# @Date  :  2024/04/26
# @Desc :
import json
import allure
from faker import Faker
import config
import conftest
from PO.page.page_login import PageLogin
from PO.page.page_user_info import PageUserinfo
from Utils.get_driver import GetDriver
from Utils.record_log import Log


@allure.feature('3、个人中心模块')
class TestEditAddr:
    def setup_class(self):
        # 获取driver
        self.driver = GetDriver().get_driver(config.login_url)
        # 异常截图
        conftest.driver = self.driver
        # 获取登录页面对象
        self.login = PageLogin(self.driver)
        # 获取用户个人中心对象
        self.userinfo = PageUserinfo(self.driver)
        # TODO 创建一个Faker对象
        self.fake = Faker(locale='zh_CN')
        # 获取日志对象
        self.log = Log("修改收货地址").get_logger()

    def teardown_class(self):
        # 关闭driver
        GetDriver().close_driver()
        Log("编辑收获地址").write_empty_line()

    @allure.story("2、编辑收货地址")
    def test_edit_addr(self):
        with allure.step("前置条件：登录测试账号"):
            # 输入用户名, 密码, 验证码, 点击登录按钮
            self.login.page_input_username(config.testdata["用户名"])
            self.login.page_input_password(config.testdata["密码"])
            self.login.page_input_verify_code(config.testdata["验证码"])
            self.login.page_click_login_btn()
            self.userinfo.base_refresh()

        with allure.step("步骤一：移动屏幕向下滑"):
            self.userinfo.base_move_scroll_bar(100)
            self.userinfo.base_get_allure_image()

        with allure.step("步骤二：点击收货地址菜单"):
            self.userinfo.page_click_address_menu()

        with allure.step("步骤三：点击增加新地址按钮"):
            self.userinfo.page_click_add_new_address_btn()

        with allure.step("步骤四：输入收货人姓名"):
            name = self.fake.name()
            self.userinfo.page_input_consignee_name(name)

        with allure.step("步骤五：输入收货人手机号"):
            phone = self.fake.phone_number()
            self.userinfo.page_input_consignee_phone(phone)

        with allure.step("步骤六：下拉框选择省份"):
            # province = self.fake.province()
            province = self.userinfo.page_select_province()
            self.userinfo.base_time_sleep(0.2)

        with allure.step("步骤七：下拉框选择城市"):
            city = self.userinfo.page_select_city()
        with allure.step("步骤八：下拉框选择区"):
            district = self.userinfo.page_select_district()
        with allure.step("步骤九：下拉框选择街道"):
            twon = self.userinfo.page_select_twon()

        with allure.step("步骤十：输入详细地址"):
            address = self.fake.building_number()
            self.userinfo.page_input_detail_address(address)

        with allure.step("步骤十一：输入邮编"):
            zipcode = self.fake.postcode()
            self.userinfo.page_input_consignee_postcode(zipcode)

        with allure.step("步骤十二：点击保存按钮"):
            self.userinfo.page_click_save_address_btn()

        with allure.step("步骤十三：点击删除按钮"):
            self.userinfo.base_refresh()
            self.userinfo.page_click_delete_address_btn()

        data = {
            "用户信息": config.testdata,
            "修改内容": {
                "收货人姓名": name,
                "收货人电话": phone,
                "省份": province,
                "城市": city,
                "区": district,
                "街道": twon,
                "详细地址": address,
                "邮编": zipcode}}
        # print(data)
        self.log.info(f"测试数据：{data}")
        data = json.dumps(data, ensure_ascii=False, indent=4)
        allure.attach(data, "测试数据", allure.attachment_type.JSON)
